package com.service.user.impl;

import com.dao.CommentDao;
import com.dao.ReportDao;
import com.entity.Comment;
import com.entity.CommentReport;
import com.entity.Notice;
import com.service.user.CommentService;
import com.service.user.NoticeService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 评论相关的实现
 *
 * 
 */
@Service
public class CommentServiceImpl implements CommentService {

  @Autowired NoticeService noticeService;

  @Override
  public int getGoodCommentCount(String goodId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    CommentDao dao = session.getMapper(CommentDao.class);
    int count = dao.selectGoodCommentCount(goodId);
    session.close();
    return count;
  }

  /**
   * 获取对应商品的评论信息
   *
   * @param goodId 商品编号
   * @return 评论列表
   */
  @Override
  public List<Comment> getGoodComments(String goodId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    CommentDao dao = session.getMapper(CommentDao.class);
    List<Comment> noDadComments = dao.selectNoDadComments(goodId);
    for (Comment floor : noDadComments) {
      floor.setChildsSon(dao.selectFloorAllComments(floor.getCommentId()));
    }
    session.close();
    return noDadComments;
  }

  /**
   * 发表评论
   *
   * @param userId 用户编号
   * @param goodId 商品编号
   * @param replyCommentId 回复评论的编号
   * @param content 内容
   * @return 新添加评论的编号
   */
  @Override
  public String addAComment(String userId, String goodId, String replyCommentId, String content) {
    String commentId = UUID.randomUUID().toString().replace("-", "");
    SqlSession session = SqlSessionGetter.getSqlSession();
    CommentDao dao = session.getMapper(CommentDao.class);
    String floorId;
    if (replyCommentId != null) {
      // 是回复别人的评论,那么本评论的楼号就应该是回复评论的楼号
      floorId = dao.selectFloorId(replyCommentId);
    } else {
      // 否则就是新建楼，楼号就是评论编号
      floorId = commentId;
    }
    dao.insertAComment(userId, goodId, commentId, replyCommentId, floorId, content);
    session.commit();
    Map<String, Object> info = dao.selectCommentInfoToNotice(commentId);
    session.close();

    // 添加通知
    noticeService.addNotice(
        (String) info.get("sellerId"),
        Notice.GOOD_BE_COMMENTED,
        null,
        0,
        userId,
        info.get("userName"),
        info.get("goodId"),
        info.get("goodName"),
        content);
    return commentId;
  }

  /**
   * 删除1条评论
   *
   * @param userId 用户编号
   * @param commentId 评论编号
   * @return 删除成功返回1，否则返回0(不是自己的评论就会失败)
   */
  @Override
  public int delAComment(String userId, String commentId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    CommentDao dao = session.getMapper(CommentDao.class);
    int row = dao.deleteAComment(userId, commentId);
    session.commit();
    session.close();
    return row;
  }

  @Override
  public int reportComment(String userId, String commentId, String type, String reason) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao reportDao = session.getMapper(ReportDao.class);
    String cpId = UUID.randomUUID().toString().replace("-", "");
    // 插入举报信息
    int ret = reportDao.insertACommentReport(cpId, userId, commentId, type, reason);
    session.commit();
    CommentReport cr = reportDao.selectCommentReportById(cpId);
    session.close();
    noticeService.addNotice(
        userId,
        Notice.COMMENT_REPORT,
        null,
        1,
        cr.getGoodId(),
        cr.getGoodName(),
        cr.getCommentContent(),
        cr.getType(),
        cr.getReason(),
        cr.getReportTime());
    return ret;
  }
}
